<template >
  <div class="b">
    <!-- Element UI -->
    <el-form
      v-loading="loading"
      element-loading-text="正在登录..."
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      :model="user"
      class="loginContainer"
      ref="formRef"
    >
      <h2 class="loginTitle">小区管理系统</h2>
      <el-form-item label="用户" prop="username">
        <el-input
          size="normal"
          type="text"
          v-model="user.uName"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          size="normal"
          type="password"
          v-model="user.uPassword"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item style="margin-left: 50px;">
        <el-button type="primary" @click="onSubmit" @keydown.enter="onSubmit">登录</el-button>
        <el-button type="primary" @click="enroll">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
  <el-dialog v-model="enrollTable" title="注册" width="30%" center>
      <el-form :model="user" label-width="120px">
        <el-form-item label="用户名">
          <el-input v-model="user.uName" />
        </el-form-item>
        <el-form-item label="密码">z
          <el-input
              type="password"
              v-model="user.uPassword" />
        </el-form-item>
      </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="enrollTable = false">取消</el-button>
        <el-button type="primary" @click="enrollConfirm"> 确认 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive,ref } from "vue";
import axios from "axios"; //发送异步请求
import router from "../router"
import { ElMessage } from 'element-plus'
import qs from "qs"; //数据转化
const user = reactive({
  uName: "",
  uPassword: "",
  // code:""
});
const formRef = ref(null)
const onSubmit = () => {

  axios.post("http://localhost:8105/user/login",qs.stringify(user)).then(res => {
     console.log(res);
    if(res.data.code == 1){
      window.localStorage.uid = res.data.data.uId
      window.localStorage.uname = res.data.data.uName
      router.push('/index')
    }else{
      ElMessage.error('用户名或密码错误')
    }
  })
};

const enrollTable = ref(false)
const enroll = () => {
  enrollTable.value = true;
}
const enrollConfirm = () =>{
  axios.post("http://localhost:8105/user/enroll",qs.stringify(user)).then(res =>{
    if (res.data.code == 1) {
      ElMessage.success("注册成功");
    } else {
      ElMessage.error("注册失败");
    }
  });
  enrollTable.value = false;
};
</script>
<style>
.b{
  background-image: url("../assets/R-C.jpg");
  width: 100%;
  height: 100%;
  background-size: cover;
  position: fixed;
}
.loginContainer {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 15px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.loginTitle {
  margin: 15px auto 20px auto;
  text-align: center;
  color: #505458;
}

.loginRemember {
  text-align: left;
  margin: 0px 0px 15px 0px;
}
.el-form-item__content {
  display: flex;
  align-items: center;
}
</style>
